<?php
class Utility {
	static public function outputArray( $array )
	{
		echo "<pre>";
		var_dump( $array );
		echo "</pre>";
	}

	//write log
	static public function writeLog($fileName, $message)
	{
		$pathName = ADMIN_ROOT.'/log/'.$fileName;

		$login = 'Test';

		if (substr($fileName, -4) != '.log')
		{
			$pathName .= '.log';
		}
		$time=date('Y-m-d H:i:s');
		$m = sprintf("%s\t%s\t%s\t%s%s", $login,getenv("REMOTE_ADDR"), $time, $message, (substr($message, -1) == "\n") ? '': "\n");
		if(@$fp = fopen($pathName, 'a'))
		{
			fwrite($fp, "$m");
			fclose($fp);
		}else{
			echo "不能读写文件：$pathName";
		}
	}

	//write error log
	static public function writeErrorLog( $message )
	{
			self::writeLog( 'error.log', $message );
	}

	static public function splitStrbySpace( $string='', $num='-1' )
	{
		if( !$string ){
			return false;
		}
		$arr = preg_split( "/\s+/", $string, $num, PREG_SPLIT_NO_EMPTY );
		return $arr;
	}


	static public function Option($a=array(), $v=null, $all=null)
	{
		$option = null;
		if ( $all ){
			$selected = ($v) ? null : 'selected';
			$option .= "<option value='' $selected>$all</option>";
		}

		$v = explode(',', $v);
		settype($v, 'array');
		foreach( $a AS $key=>$value )
		{
			$selected = in_array($key, $v) ? 'selected' : null;
			$option .= "<option value='$key' $selected>$value</option>";
		}

		return $option;
	}

	static public function GetColumn($a=array(), $column='id')
	{
		$ret = array();
		foreach( $a AS $one )
		{
			$ret[] = @$one[ $column ];
		}

		return $ret;
	}

	/* support 2-level now */
	static public function AssColumn($a=array(), $column='id')
	{
		$two_level = func_num_args() > 2 ? true : false;
		if ( $two_level )
			$scolumn = func_get_arg(2);

		$ret = array();
		if ( false == $two_level )
		{
			foreach( $a AS $one )
			{
				$ret[ @$one[$column] ] = $one;
			}
		}
		else
		{
			foreach( $a AS $one )
			{
				if ( false==isset( $ret[ @$one[$column] ] ) )
					$ret[ @$one[$column] ] = array();

				$ret[ @$one[$column] ][ @$one[$scolumn] ] = $one;
			}
		}
		return $ret;
	}
}
?>
